package com.zby.ss.core.util;

import com.zby.ss.core.constants.StandardBasicTypes;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created By zby on 22:31 2019/2/25
 * 将bean文件中的value值转化为属性值
 */

public final class Transfomer {

    public final static Integer MAX_BYTE = 127;

    public final static Integer MIN_BYTE = -128;

    public final static Integer MAX_SHORT = 32767;

    public final static Integer MIN_SHORT = -32768;

    public final static String STR_TRUE = "true";

    public final static String STR_FALSE = "false";


    /**
     * Created By zby on 22:57 2019/3/2
     * 数据转化
     */

    /**
     * Created By zby on 22:32 2019/2/25
     * 数据转化
     *
     * @param typeName 属性类型的名字
     * @param value    值
     */
    public static Object transformerPropertyValue(String typeName, Object value) throws IllegalAccessException {
        if (StringUtils.isBlank(typeName)) {
            throw new RuntimeException("属性的类型不能为空typeName+" + typeName);
        }
        if (typeName.equals(StandardBasicTypes.STRING)) {
            return objToString(value);
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.LONG)) {
            return stringToLong(objToString(value));
        } else if (typeName.equals(StandardBasicTypes.INTEGER) || typeName.equals(StandardBasicTypes.INT)) {
            return stringToInt(objToString(value));
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.BYTE)) {
            return stringToByte(objToString(value));
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.SHORT)) {
            return stringToShort(objToString(value));
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.BOOLEAN)) {
            return stringToBoolean(objToString(value));
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.DOUBLE)) {
            return stringToDouble(objToString(value));
        } else if (typeName.equalsIgnoreCase(StandardBasicTypes.FLOAT)) {
            return stringToFloat(objToString(value));
        } else if (typeName.equals(StandardBasicTypes.DATE)) {
            return stringToDate(objToString(value));
        } else if (typeName.equals(StandardBasicTypes.BIG_DECIMAL)) {
            return stringToBigDecimal(objToString(value));
        } else {
            return value;
        }
    }


    /**
     * Created By zby on 22:32 2019/2/25
     * 数据转化
     */
    public static void transformerPropertyValue(Object currentObj, Field field, Object value) throws IllegalAccessException {
        if (null == currentObj && field == null) {
            throw new RuntimeException("当前对象或属性为空值");
        }
        String typeName = field.getType().getSimpleName();
        field.setAccessible(true);
        field.set(currentObj, transformerPropertyValue(typeName, value));
    }

    /**
     * Created By zby on 23:29 2019/2/25
     * obj to String
     */
    public static String objToString(Object obj) {
        return null == obj ? null : obj.toString();
    }

    /**
     * Created By zby on 23:54 2019/2/25
     * String to integer
     */
    public static Integer stringToInt(String val) {
        if (StringUtils.isBlank(val)) {
            return 0;
        }
        if (val.charAt(0) == 0) {
            throw new RuntimeException("字符串转为整形失败val=" + val);
        }
        return Integer.valueOf(val);
    }

    /**
     * Created By zby on 23:31 2019/2/25
     * String to Long
     */
    public static Long stringToLong(String val) {
        return Long.valueOf(stringToInt(val));
    }

    /**
     * Created By zby on 23:52 2019/2/26
     * String to byte
     */
    public static Short stringToShort(String val) {
        Integer result = stringToInt(val);
        if (result >= MIN_SHORT && result <= MAX_SHORT) {
            return Short.valueOf(result.toString());
        }
        throw new RuntimeException("数据转化失败result=" + result);
    }

    /**
     * Created By zby on 0:03 2019/2/27
     * String to short
     */
    public static Byte stringToByte(String val) {
        Integer result = stringToInt(val);
        if (result >= MIN_BYTE && result <= MAX_BYTE) {
            return Byte.valueOf(result.toString());
        }
        throw new RuntimeException("数据转化失败result=" + result);
    }

    /**
     * Created By zby on 0:20 2019/2/27
     * string to double
     */
    public static Double stringToDouble(String val) {
        if (StringUtils.isBlank(val)) {
            throw new RuntimeException("数据为空，转换失败");
        }
        return Double.valueOf(val);
    }

    /**
     * Created By zby on 0:23 2019/2/27
     * string to float
     */
    public static Float stringToFloat(String val) {
        if (StringUtils.isBlank(val)) {
            throw new RuntimeException("数据为空，转换失败");
        }
        return Float.valueOf(val);
    }

    /**
     * Created By zby on 0:19 2019/2/27
     * string to boolean
     */
    public static boolean stringToBoolean(String val) {
        if (StringUtils.isBlank(val)) {
            throw new RuntimeException("数据为空，转换失败val=" + val);
        }
        if (val.equals(STR_TRUE)) {
            return true;
        }
        if (val.equals(STR_FALSE)) {
            return false;
        }
        byte result = stringToByte(val);
        if (0 == result) {
            return false;
        }
        if (1 == result) {
            return true;
        }
        throw new RuntimeException("数据转换失败val=" + val);
    }


    /**
     * Created By zby on 0:24 2019/2/27
     * string to Date
     */
    public static Date stringToDate(String val) {
        if (StringUtils.isBlank(val)) {
            throw new RuntimeException("数据为空，转换失败val=" + val);
        }
        SimpleDateFormat format = new SimpleDateFormat();
        try {
            return format.parse(val);
        } catch (ParseException e) {
            throw new RuntimeException("字符串转为时间失败val=" + val);
        }
    }

    /**
     * Created By zby on 0:31 2019/2/27
     * string to big decimal
     */
    public static BigDecimal stringToBigDecimal(String val) {
        if (StringUtils.isBlank(val)) {
            throw new RuntimeException("数据为空，转换失败val=" + val);
        }
        return new BigDecimal(stringToDouble(val));
    }

}
